package com.third.clock.ui.model.presenter;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.third.clock.bean.LoginEntity;
import com.third.clock.bean.ResponseData;
import com.third.clock.common.CodeEnum;
import com.third.clock.common.Contants;
import com.third.clock.common.LogUtils;
import com.third.clock.common.SharedPreferencesUtils;
import com.third.clock.manager.ClockManager;
import com.third.clock.manager.OkHttpClientManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.ui.model.ILoginView;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class LoginPresenter {

    private Context mContext;

    private ILoginView mILoginView;

    private Handler handler=new Handler();

    private ClockManager mClockManager;

    private SharedPreferencesUtils mSharedPreferencesUtils;

    public LoginPresenter(Context context){
        this.mContext=context;
        mClockManager=ClockManager.getInstace(context);
        mSharedPreferencesUtils=SharedPreferencesUtils.getInstance(context);
    }

    public void setILoginView(ILoginView mView){
        this.mILoginView=mView;
    }

    public void login(String username,String pwd){
        if(username!=null&&pwd!=null){
            if(TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){
                if(mILoginView!=null) mILoginView.onLoginFail("用户名，密码不能为空");
                return;
            }
            if(!TransformManager.isMobileNO(username)){
                if(mILoginView!=null) mILoginView.onLoginFail("请输入正确的手机号");
                return;
            }
            if(!TransformManager.isPassword(pwd)){
                if(mILoginView!=null) mILoginView.onLoginFail("密码必须是6位纯数字");
                return;
            }
           if(mILoginView!=null) mILoginView.onLoginning();
            mSharedPreferencesUtils.putStringValue(SharedPreferencesUtils.PASSWORD_KEY,pwd);
            mSharedPreferencesUtils.putStringValue(SharedPreferencesUtils.USERNAME_KTY,username);
            mClockManager.login(new OkHttpClientManager.okHttpCallback() {
                @Override
                public void onFailure(com.lzy.okgo.model.Response<String> s) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            mILoginView.onHideLoginning();
                            mILoginView.onLoginFail("登录失败");
                        }
                    });
                }

                @Override
                public void onResponse(com.lzy.okgo.model.Response<String> response) {
                    try {
                        if(response!=null){
                            if(response.body()!=null) {
                                String result = response.body();
                                Logger.json(result);
                                LogUtils.e("login",""+result);
                                final ResponseData<LoginEntity> mLoginResponseData = TransformManager.paserLogin(response.body());
                                if(mLoginResponseData!=null){
                                    if(mLoginResponseData.getCode()== Contants.CODE_0){
                                        mILoginView.onLoginSuccess();
                                    }else{
                                        handler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                CodeEnum codeEnum=CodeEnum.get(mLoginResponseData.getCode());
                                                if(codeEnum!=null)mILoginView.onLoginFail(codeEnum.codetext);
                                            }
                                        });
                                    }
                                }
                            }
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mILoginView.onHideLoginning();
                            }
                        });

                    }catch (Exception e){
                        if(mILoginView!=null) {
                            mILoginView.onHideLoginning();
                            mILoginView.onLoginFail("登录异常");
                        }
                    }
                }
            });


        }
    }


}
